import Vue from 'vue';
import axios from 'axios';

const qs = require('qs');

export default function _axios(url: any,options:any):any {
      var opt = options || {};
      return new Promise((resolve, reject) => {
          axios({
              method: opt.type || 'post',
              url: url,
              params: opt.params || {},
              // 判断是否有自定义头部，以对参数进行序列化。不定义头部，默认对参数序列化为查询字符串。
              data: (opt.headers ? opt.data :qs.stringify(opt.data)) || {},
              responseType: opt.dataType || 'json',
              // 设置默认请求头
              headers: opt.headers || {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}
          }).then(response => {

              if (response.data.code === 0) {
                  resolve(response.data)
              } else if (response.data.code === '000') {
                  resolve(response.data)
              } else {
                  reject(response.data)
                  //store.commit('SET_LOADING', false)
              }
          }).catch(error => {
              reject(error)
              //store.commit('SET_LOADING', false)
          })
      })
  }





